/**
 * 临时数据存储接口测试用例
 *
 * Created by liuxuechao on 2022/08/09.
 */
const chai = require('chai');
const should = chai.should(); // eslint-disable-line no-unused-vars
chai.use(require('chai-http'));

const common = require('./../../common');
const Administrator = require('./../../../models/administrator');
const uri = require('./../uri');
const data = require('./../../../benchmark/ephemeral-data/data.json');

module.exports = describe('/POST ephemeral-data.json', function () {
  const url = '/ephemeral-data.json';
  const administrator = new Administrator();
  const newUserIds = [];
  let tester;

  before(function (done) {
    administrator.createRandomTester()
      .then((user) => {
        tester = user;
        newUserIds.push(user.userId);
        done();
      })
      .catch(done);
  });
  after(function (done) {
    administrator.removeUsers(newUserIds)
      .then(() => administrator.logout())
      .then(() => {
        done();
      })
      .catch(done);
  });

  common.should.be.user(uri, url, 'post');
  common.should.be.tester(uri, url, 'post');

  // 正常发起请求
  it('it should storage ephemeral data', function (done) {
    tester.login()
      .then(() => {
        chai.request(uri)
          .post(url)
          .set('Authorization', `Bearer ${tester.token.accessToken}`)
          .send({ data })
          .end((err, res) => {
            res.should.have.status(200);
            common.should.be.ephemeralMessage(res.body);
            done();
          });
      })
      .catch(done);
  });

  // 数据字段缺失
  it('it should fail to storage when lossing data field', function (done) {
    tester.login()
      .then(() => {
        chai.request(uri)
          .post(url)
          .set('Authorization', `Bearer ${tester.token.accessToken}`)
          .send({})
          .end((err, res) => {
            res.should.have.status(400);
            common.should.be.errorMessage(res.body);
            done();
          });
      })
      .catch(done);
  });
});
